<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="./imgs/favicon.png" type="image/x-icon">
    <link rel="preload" href="./js/vue@2.js" as="script">
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/search.css">
    <link rel="shortcut icon" href="./favicon.png" type="image/x-icon">
    <script async="async" src="https://www.google.com/adsense/search/ads.js"></script>
    <title>Goodlifestylenow</title>
    <script type="text/javascript" charset="utf-8">
        (function (g, o) {
            g[o] = g[o] || function () {
                (g[o]['q'] = g[o]['q'] || []).push(
                    arguments)
            }, g[o]['t'] = 1 * new Date
        })(window, '_googCsa');
    </script>
</head>

<body>
    <div id="app" v-cloak>
        <div class="container">
            <a class="home" href="./">
                <img src="./imgs/logo.png" alt="logo">
            </a>
            <div class="search">
                <div class="search-container">
                    <div class="search-input">
                        <input placeholder="Search..." type="text" v-model="searchPageWords"
                            @keyup.enter="handleToSearch">
                    </div>
                    <div class="search-clear">
                        <img src="./imgs/close.svg" alt="close">
                    </div>
                    <div class="search-btn" @click="handleToSearch">
                        <img src="./imgs/main-search.svg" alt="search">
                    </div>
                </div>
            </div>
            <div class="container-main">
                <div id="afscontainer1"></div>
                <div class="search-result">
                    <span class="search-result_span">SEARCH RESULTS</span>
                    <ul>
                        <li v-for="item in list" :key="item.id">
                            <div>
                                <div class="search-result-top">
                                    <a :href="handleToDetail(item.siteName, item.id)" class="search-result-top-desc">
                                        <span class="search-result-domain">{{item.name}}</span>
                                    </a>
                                </div>
                                <div class="search-result-title">
                                    <a :href="handleToDetail(item.siteName, item.id)">{{item.title}}</a>
                                </div>
                                <p class="search-result-info">{{item.information}}</p>
                            </div>
                        </li>
                        <li v-for="item in mock" :key="item.id">
                            <div>
                                <div class="search-result-top">
                                    <a :href="handleToDetail(item.siteName, item.id)" class="search-result-top-desc">
                                        <span class="search-result-domain">{{item.name}}</span>
                                    </a>
                                </div>
                                <div class="search-result-title">
                                    <a :href="handleToDetail(item.siteName, item.id)">{{item.title}}</a>
                                </div>
                                <p class="search-result-info">{{item.information}}</p>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <a-footer></a-footer>
    </div>
</body>
<script src="./main.js"></script>
<script src="./mock.js"></script>
<script src="./js/vue@2.js"></script>
<script type="module">
    import { footer } from './components/index.js'
    var vm = new Vue({
        el: '#app',
        components: {
            aFooter: footer
        },
        data() {
            return {
                list: [],
                keywords: '',
                searchPageWords: '',
                mock
            }
        },
        created() {
            // this.keywordsAdd()
            this.searchPageWords = this.keywords = this.getQueryString('keywords') ? this.getQueryString('keywords') : ''
            this.handleFetchSearchResult()
            document.title = this.searchPageWords
        },
        methods: {
            getQueryString(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
                var r = window.location.search.substr(1).match(reg);

                if (r != null) {
                    var result = r[2];
                    result = result.replace(/\+/g, ' ');
                    return decodeURIComponent(result);
                }
                return null;
            },
            keywordsAdd() {
                fetch('https://migame.vip/gamefront/keyword/add', {
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    method: 'post',
                    body: JSON.stringify({
                        informationId: 2210,
                        keywords: this.getQueryString('keywords'),
                        channelId: 8054
                    })
                })
                    .then(res => res.json())
                    .then(res => {

                    })
                    .catch(err => {
                        throw new Error(err)
                    })
            },
            handleToDetail(siteName, id) {
                return `./detail.html?id=${id}`
            },
            loadAfs() {
                const url = new URL(location.href);
                var pageOptions = {
                    // "adtest": "on",
                    "hl": "en", // 地区代码默认 en
                    "adPage": 1,
                    "pubId": "pub-8180331903483073",
                    // "channel": "7359065559",
                    "styleId": "3875934019",
                    "query": this.keywords,
                    "adsafe": "low",
                    "resultsPageBaseUrl": "https://www.goodlifestylenow.com/search.html" + url.search,
                    "ivt": false,
                    // Enter the base url for your results page
                    "resultsPageQueryParam": "keywords",// (Default to "q") Matches the param denoting the query on the search page
                    "adLoadedCallback": function (containerName, adsLoaded,
                        isExperimentVariant, callbackOptions) {
                        if (adsLoaded) {
                            try {
                                console.log(1);
                                // 很可能不执行任何操作
                            } catch (e) {
                                alert("回调函数中存在错误");
                                // 执行某些操作，以妥善处理错误
                            }
                        } else {
                            // console.log(2);
                            // _googCsa('ads', pageOptions, adblock1);
                            // 采用 Google 无法提供任何广告时的一贯做法
                        }
                    }
                };
                var adblock1 = {
                    "container": "afscontainer1",
                    "maxTop": "2"
                };
                _googCsa('ads', pageOptions, adblock1);
            },
            handleToSearch() {
                this.keywords = this.searchPageWords
                window.location.href = `./search.html?keywords=${this.keywords}`
            },
            handleFetchSearchResult() {
                fetch('https://migame.vip/gamefront/blog/searchForBlog', {
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    method: 'post',
                    body: JSON.stringify({
                        keyWord: this.keywords
                    })
                })
                    .then(res => res.json())
                    .then(res => {
                        if (res.code == 0) {
                            this.list = res.data
                        }
                        this.$nextTick(this.loadAfs)
                    })
                    .catch(err => {
                        throw new Error(err)
                    })
            }
        }
    })
</script>
<script>
    var pattern = /^master/;
    window.addEventListener('blur', function () {
        if (document.activeElement.tagName.toLowerCase() == 'iframe') {
            gtag('event', 'click_ad', {
                'event_name': 'click_ad'
            })
        }
    })
</script>

</html>